﻿using MediatR;
using Micro.Extensions.SqlSugar.Base;
using Micro.Modules.Abstractions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Module.Test.Application.AutoMapper;
using Module.Test.Application.GrpcServices;
using Module.Test.Application.IServices;
//using Module.Test.Application.RefitServices;
using Module.Test.Application.Services;
using Module.Test.Domain.CommandHandlers;
using Module.Test.Domain.Commands.Customs;
using Module.Test.Domain.EventHandlers;
using Module.Test.Domain.Events.Customs;
using Module.Test.Domain.IRepositorys;
using Module.Test.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Micro.Extensions.Refit;

namespace Module.Test.Api
{
    public class ModuleExtensions : BaseModule
    {
        public override IServiceCollection AddModuleServices(IServiceCollection services, IModule module)
        {

            //services.AddScoped<ICustomAppService, CustomAppService>();
            services.AddAutoMapper(typeof(AutoMapperProfile));

            // Domain - Events
            //services.AddScoped<INotificationHandler<RegisteredCustomEvent>, CustomEventHandler>();
            //services.AddScoped<INotificationHandler<UpdatedCustomEvent>, CustomEventHandler>();
            //services.AddScoped<INotificationHandler<RemovedCustomEvent>, CustomEventHandler>();

            // Domain - Commands
            //services.AddScoped<IRequestHandler<RegisterCustomCommand, bool>, CustomCommandHandler>();
            //services.AddScoped<IRequestHandler<UpdateCustomCommand, bool>, CustomCommandHandler>();
            //services.AddScoped<IRequestHandler<RemoveCustomCommand, bool>, CustomCommandHandler>();


            //services.AddScoped<ICustomRepository, CustomRepository>();//注册仓储

            //注册Refit，默认Refit配置，如果IRefitCustomAppService继承IRefit，不需要添加
            //services.AddRefitServices(typeof(IRefitCustomAppService));

            //模块自定义RefitOption，模块自定义不能Refit配置名
            //RefitOption refitOption = services.GetConfiguration().Get<RefitOption>("TestRefit");//AppsettingConfigs.get<RefitOption>("TestRefit");
            //services.AddRefitServices(refitOption);
            return base.AddModuleServices(services, module);
        }

        public override void Configure(IApplicationBuilder app, IModule module)
        {
            //grpc server
            IEndpointRouteBuilder endpointRouteBuilder = (app as WebApplication)!;
            endpointRouteBuilder.MapGrpcService<PersonService>();
            base.Configure(app, module);
        }
    }
}
